'''
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Created on Jul 25, 2009

Version: 

@author: bryan
'''
#===============================================================================
# Imports
#===============================================================================
import JT

class FixedImage():
    """
    A 2D vtk image class.  Provides methods to load an image into memory.
    """
    def __init__(self):
        self._imageSequence = []
        self._filename = None
        self._image = None
        # Create fixed image masking components
        self._maskReader = JT.vtk.vtkMetaImageReader()
        self._mask = JT.vtk.vtkImageMask()
        
    
    def SetFileName(self,filename):
        """
        Use the generic VTK image file reader which supports most common
        formats (mhd, jpg, bmp, tif, png,).  It can handle different pixel
        types that are allowed by the image format (Char, short, float, etc).
        
        An image sequence is NO LONGER maintained in this class.  Only one 
        image is loaded at a time.  Multiple images are managed by the Data 
        class.
        """
        self._filename = filename
        image = JT.utils.genericImageReader(filename)
        self.SetImage(image)
        
    def AddImage(self,image):
        """
        DEPRECATED.  Use SetImage instead.
        """
        print 'DEPRECATED.  Use SetImage instead.'
        self.SetImage(image)
    
    def SetImage(self,image):
        """
        Set a VTK image to be the current fixed image for registration.
        """
        try:
            assert(image.GetClassName(), 'vtkImageData')
        except:
            print 'Image supplied is not in VTK image format, try again'
            raise
        self._image = image
        
    def RemoveAllImages(self):
        """
        DEPRECATED. Remove all images loaded into self._imageSequence
        """
        print "Deprecated. Only one image is stored in this class."
        self._imageSequence = []
        self._filename = []
        
    def SetFixedImageMask(self,filename):
        """
        Set the image mask.  Needs to be modified for image sequence.
        """
        if self._fixedImage:
            print "Need to rewrite to change itk code to vtk."
            raise AttributeError
#            self._maskReader.SetFileName(filename)
#            self._maskReader.Update()
#            self._mask.SetMaskInput(self._maskReader.GetOutput())
#            self._itk2vtk.SetInput(self._fixedImage)
#            self._mask.SetImageInput(self._itk2vtk.GetOutput())
#            self._vtk2itk.SetInput(self._mask.GetOutput())
#            self._fixedImage = self._vtk2itk.GetOutput()
#            self._pyFixedImage = vtk2numpy(self._fixedImage)
        else:
            print "Fixed image must be set before mask."

    
    def GetImage(self,i=None):
        """
        Return the current VTK image.  index parameter i is no 
        longer used (only one image es stored in this class, 
        not an image sequence).
        """
        if i is not None:
            print "Only one image stored in this class, not a sequence,\
                   update your code."
        return self._image    
    
        
    def GetRegion(self,roi):
        """will return the numpy image data within the specified ROI.
        """
        py_img = JT.utils.vtk2numpy(self.GetImage())
        roi_img = py_img[roi[0][0]:roi[1][0], roi[0][1]:roi[1][1]]
        return roi_img
        
